Added experimental support for restoration
authorTim Starling <tstarling@users.mediawiki.org>
Mon, 24 Apr 2006 08:49:29 +0000 (08:49 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Mon, 24 Apr 2006 08:49:29 +0000 (08:49 +0000)
maintenance/storage/checkStorage.php

index 665f3d0..a83d274 100644 (file)
@@ -8,316 +8,461 @@ define( 'CONCAT_HEADER', 'O:27:"concatenatedgziphistoryblob"' );
 \r
 if ( !defined( 'MEDIAWIKI' ) ) {\r
        require_once( dirname(__FILE__) . '/../commandLine.inc' );\r
-       require_once('ExternalStore.php');\r
+       require_once( 'ExternalStore.php' );\r
        require_once( 'ExternalStoreDB.php' );\r
+       require_once( 'SpecialImport.php' );\r
 \r
-       checkStorage();\r
+       $cs = new CheckStorage;\r
+       $fix = isset( $options['fix'] );\r
+       if ( isset( $args[0] ) ) {\r
+               $xml = $args[0];\r
+       } else {\r
+               $xml = false;\r
+       }\r
+       $cs->check( $fix, $xml );\r
 }\r
 \r
 \r
 //----------------------------------------------------------------------------------\r
 \r
-function checkStorage() {\r
-       global $oldIdMap, $brokenRevisions;\r
-       \r
-       $fname = 'checkStorage';\r
-       $dbr =& wfGetDB( DB_SLAVE );\r
-       $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, $fname );\r
-       $chunkSize = 1000;\r
-       $flagStats = array();\r
-       $objectStats = array();\r
-       $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' );\r
-       $dbStore = null;\r
-       $brokenRevisions = array();\r
-\r
-       for ( $chunkStart = 1 ; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {\r
-               $chunkEnd = $chunkStart + $chunkSize - 1;\r
-               //print "$chunkStart of $maxRevId\n";\r
-\r
-               // Fetch revision rows\r
-               $oldIdMap = array();\r
-               $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ), \r
-                       array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), $fname );\r
-               while ( $row = $dbr->fetchObject( $res ) ) {\r
-                       $oldIdMap[$row->rev_id] = $row->rev_text_id;\r
-               }\r
-               $dbr->freeResult( $res );\r
-\r
-               if ( !count( $oldIdMap ) ) {\r
-                       continue;\r
+class CheckStorage\r
+{\r
+       var $oldIdMap, $errors;\r
+       var $dbStore = null;\r
+\r
+       var $errorDescriptions = array(\r
+               'restore text' => 'Damaged text, need to be restored from a backup',\r
+               'restore revision' => 'Damaged revision row, need to be restored from a backup',\r
+               'unfixable' => 'Unexpected errors with no automated fixing method',\r
+               'fixed' => 'Errors already fixed',\r
+               'fixable' => 'Errors which would already be fixed if --fix was specified',\r
+       );              \r
+\r
+       function check( $fix = false, $xml = '' ) {\r
+               $fname = 'checkStorage';\r
+               $dbr =& wfGetDB( DB_SLAVE );\r
+               if ( $fix ) {\r
+                       $dbw =& wfGetDB( DB_MASTER );\r
+                       print "Checking, will fix errors if possible...\n";\r
+               } else {\r
+                       print "Checking...\n";\r
                }\r
-\r
-               // Fetch old_flags\r
-               $missingTextRows = array_flip( $oldIdMap );\r
-               $externalRevs = array();\r
-               $objectRevs = array();\r
-               $flagsFields = array();\r
-               $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ), \r
-                       'old_id IN (' . implode( ',', $oldIdMap ) . ')', $fname );\r
-               while ( $row = $dbr->fetchObject( $res ) ) {\r
-                       $flags = $row->old_flags;\r
-                       $id = $row->old_id;\r
-\r
-                       // Create flagStats row if it doesn't exist\r
-                       $flagStats = $flagStats + array( $flags => 0 );\r
-                       // Increment counter\r
-                       $flagStats[$flags]++;\r
-\r
-                       // Not missing\r
-                       unset( $missingTextRows[$row->old_id] );\r
-\r
-                       // Check for external or object\r
-                       if ( $flags == '' ) {\r
-                               $flagArray = array();\r
-                       } else {\r
-                               $flagArray = explode( ',', $flags );\r
-                       }\r
-                       if ( in_array( 'external', $flagArray ) ) {\r
-                               $flagsFields[$id] = $flags; // is this needed?\r
-                               $externalRevs[] = $id;\r
-                       } elseif ( in_array( 'object', $flagArray ) ) {\r
-                               $flagsFields[$id] = $flags; // is this needed?\r
-                               $objectRevs[] = $id;\r
+               $maxRevId = $dbr->selectField( 'revision', 'MAX(rev_id)', false, $fname );\r
+               $chunkSize = 1000;\r
+               $flagStats = array();\r
+               $objectStats = array();\r
+               $knownFlags = array( 'external', 'gzip', 'object', 'utf-8' );\r
+               $this->errors = array(\r
+                       'restore text' => array(),\r
+                       'restore revision' => array(),\r
+                       'unfixable' => array(),\r
+                       'fixed' => array(),\r
+                       'fixable' => array(),\r
+               );\r
+\r
+               for ( $chunkStart = 1 ; $chunkStart < $maxRevId; $chunkStart += $chunkSize ) {\r
+                       $chunkEnd = $chunkStart + $chunkSize - 1;\r
+                       //print "$chunkStart of $maxRevId\n";\r
+\r
+                       // Fetch revision rows\r
+                       $this->oldIdMap = array();\r
+                       $dbr->ping();           \r
+                       $res = $dbr->select( 'revision', array( 'rev_id', 'rev_text_id' ), \r
+                               array( "rev_id BETWEEN $chunkStart AND $chunkEnd" ), $fname );\r
+                       while ( $row = $dbr->fetchObject( $res ) ) {\r
+                               $this->oldIdMap[$row->rev_id] = $row->rev_text_id;\r
                        }\r
+                       $dbr->freeResult( $res );\r
 \r
-                       // Check for unrecognised flags\r
-                       if ( count( array_diff( $flagArray, $knownFlags ) ) ) {\r
-                               checkError( "Warning: invalid flags field \"$flags\"", $id );\r
+                       if ( !count( $this->oldIdMap ) ) {\r
+                               continue;\r
                        }\r
-               }\r
-               $dbr->freeResult( $res );\r
 \r
-               // Output errors for any missing text rows\r
-               foreach ( $missingTextRows as $oldId => $revId ) {\r
-                       print "Error: missing text row $oldId for revision $revId\n";\r
-               }\r
-\r
-               // Verify external revisions\r
-               $externalConcatBlobs = array();\r
-               $externalNormalBlobs = array();\r
-               if ( count( $externalRevs ) ) {\r
-                       $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ), \r
-                               array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), $fname );\r
+                       // Fetch old_flags\r
+                       $missingTextRows = array_flip( $this->oldIdMap );\r
+                       $externalRevs = array();\r
+                       $objectRevs = array();\r
+                       $res = $dbr->select( 'text', array( 'old_id', 'old_flags' ), \r
+                               'old_id IN (' . implode( ',', $this->oldIdMap ) . ')', $fname );\r
                        while ( $row = $dbr->fetchObject( $res ) ) {\r
-                               $urlParts = explode( '://', $row->old_text, 2 );\r
-                               if ( count( $urlParts ) !== 2 || $urlParts[1] == '' ) {\r
-                                       checkError( "Error: invalid URL \"{$row->old_text}\"", $row->old_id );\r
-                                       continue;\r
+                               $flags = $row->old_flags;\r
+                               $id = $row->old_id;\r
+\r
+                               // Create flagStats row if it doesn't exist\r
+                               $flagStats = $flagStats + array( $flags => 0 );\r
+                               // Increment counter\r
+                               $flagStats[$flags]++;\r
+\r
+                               // Not missing\r
+                               unset( $missingTextRows[$row->old_id] );\r
+\r
+                               // Check for external or object\r
+                               if ( $flags == '' ) {\r
+                                       $flagArray = array();\r
+                               } else {\r
+                                       $flagArray = explode( ',', $flags );\r
                                }\r
-                               list( $proto, $path ) = $urlParts;\r
-                               if ( $proto != 'DB' ) {\r
-                                       checkError( "Error: invalid external protocol \"$proto\"", $row->old_id );\r
-                                       continue;\r
+                               if ( in_array( 'external', $flagArray ) ) {\r
+                                       $externalRevs[] = $id;\r
+                               } elseif ( in_array( 'object', $flagArray ) ) {\r
+                                       $objectRevs[] = $id;\r
                                }\r
-                               $path = explode( '/', $row->old_text );\r
-                               $cluster = $path[2];\r
-                               $id = $path[3];\r
-                               if ( isset( $path[4] ) ) {\r
-                                       $externalConcatBlobs[$cluster][$id][] = $row->old_id;\r
-                               } else {\r
-                                       $externalNormalBlobs[$cluster][$id][] = $row->old_id;\r
+\r
+                               // Check for unrecognised flags\r
+                               if ( $flags == '0' ) {\r
+                                       // This is a known bug from 2004\r
+                                       // It's safe to just erase the old_flags field\r
+                                       if ( $fix ) {\r
+                                               $this->error( 'fixed', "Warning: old_flags set to 0", $id );\r
+                                               $dbw->ping();\r
+                                               $dbw->update( 'text', array( 'old_flags' => '' ), \r
+                                                       array( 'old_id' => $id ), $fname );\r
+                                               echo "Fixed\n";\r
+                                       } else {\r
+                                               $this->error( 'fixable', "Warning: old_flags set to 0", $id );\r
+                                       }\r
+                               } elseif ( count( array_diff( $flagArray, $knownFlags ) ) ) {\r
+                                       $this->error( 'unfixable', "Error: invalid flags field \"$flags\"", $id );\r
                                }\r
                        }\r
                        $dbr->freeResult( $res );\r
-               }\r
 \r
-               // Check external concat blobs for the right header\r
-               checkExternalConcatBlobs( $externalConcatBlobs );\r
-               \r
+                       // Output errors for any missing text rows\r
+                       foreach ( $missingTextRows as $oldId => $revId ) {\r
+                               $this->error( 'restore revision', "Error: missing text row", $oldId );\r
+                       }\r
 \r
-               // Check external normal blobs for existence\r
-               if ( count( $externalNormalBlobs ) ) {\r
-                       if ( is_null( $dbStore ) ) {\r
-                               $dbStore = new ExternalStoreDB;\r
+                       // Verify external revisions\r
+                       $externalConcatBlobs = array();\r
+                       $externalNormalBlobs = array();\r
+                       if ( count( $externalRevs ) ) {\r
+                               $res = $dbr->select( 'text', array( 'old_id', 'old_flags', 'old_text' ), \r
+                                       array( 'old_id IN (' . implode( ',', $externalRevs ) . ')' ), $fname );\r
+                               while ( $row = $dbr->fetchObject( $res ) ) {\r
+                                       $urlParts = explode( '://', $row->old_text, 2 );\r
+                                       if ( count( $urlParts ) !== 2 || $urlParts[1] == '' ) {\r
+                                               $this->error( 'restore text', "Error: invalid URL \"{$row->old_text}\"", $row->old_id );\r
+                                               continue;\r
+                                       }\r
+                                       list( $proto, $path ) = $urlParts;\r
+                                       if ( $proto != 'DB' ) {\r
+                                               $this->error( 'restore text', "Error: invalid external protocol \"$proto\"", $row->old_id );\r
+                                               continue;\r
+                                       }\r
+                                       $path = explode( '/', $row->old_text );\r
+                                       $cluster = $path[2];\r
+                                       $id = $path[3];\r
+                                       if ( isset( $path[4] ) ) {\r
+                                               $externalConcatBlobs[$cluster][$id][] = $row->old_id;\r
+                                       } else {\r
+                                               $externalNormalBlobs[$cluster][$id][] = $row->old_id;\r
+                                       }\r
+                               }\r
+                               $dbr->freeResult( $res );\r
                        }\r
-                       foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {\r
-                               $blobIds = array_keys( $xBlobIds );\r
-                               $extDb =& $dbStore->getSlave( $cluster );\r
-                               $blobsTable = $dbStore->getTable( $extDb );\r
-                               $res = $extDb->select( $blobsTable, \r
-                                       array( 'blob_id' ), \r
-                                       array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );\r
-                               while ( $row = $extDb->fetchObject( $res ) ) {\r
-                                       unset( $xBlobIds[$row->blob_id] );\r
+\r
+                       // Check external concat blobs for the right header\r
+                       $this->checkExternalConcatBlobs( $externalConcatBlobs );\r
+                       \r
+                       // Check external normal blobs for existence\r
+                       if ( count( $externalNormalBlobs ) ) {\r
+                               if ( is_null( $this->dbStore ) ) {\r
+                                       $this->dbStore = new ExternalStoreDB;\r
                                }\r
-                               $extDb->freeResult( $res );\r
-                               // Print errors for missing blobs rows\r
-                               foreach ( $xBlobIds as $blobId => $oldId ) {\r
-                                       checkError( "Error: missing target $blobId for one-part ES URL", $oldId );\r
+                               foreach ( $externalConcatBlobs as $cluster => $xBlobIds ) {\r
+                                       $blobIds = array_keys( $xBlobIds );\r
+                                       $extDb =& $this->dbStore->getSlave( $cluster );\r
+                                       $blobsTable = $this->dbStore->getTable( $extDb );\r
+                                       $res = $extDb->select( $blobsTable, \r
+                                               array( 'blob_id' ), \r
+                                               array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );\r
+                                       while ( $row = $extDb->fetchObject( $res ) ) {\r
+                                               unset( $xBlobIds[$row->blob_id] );\r
+                                       }\r
+                                       $extDb->freeResult( $res );\r
+                                       // Print errors for missing blobs rows\r
+                                       foreach ( $xBlobIds as $blobId => $oldId ) {\r
+                                               $this->error( 'restore text', "Error: missing target $blobId for one-part ES URL", $oldId );\r
+                                       }\r
                                }\r
                        }\r
-               }\r
 \r
-               // Check local objects\r
-               $dbr->ping();\r
-               $concatBlobs = array();\r
-               $curIds = array();\r
-               if ( count( $objectRevs ) ) {\r
-                       $headerLength = 300;\r
-                       $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), \r
-                               array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), $fname );\r
-                       while ( $row = $dbr->fetchObject( $res ) ) {\r
-                               $oldId = $row->old_id;\r
-                               if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header, $matches ) ) {\r
-                                       checkError( "Error: invalid object header", $oldId );\r
-                                       continue;\r
-                               }\r
+                       // Check local objects\r
+                       $dbr->ping();\r
+                       $concatBlobs = array();\r
+                       $curIds = array();\r
+                       if ( count( $objectRevs ) ) {\r
+                               $headerLength = 300;\r
+                               $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), \r
+                                       array( 'old_id IN (' . implode( ',', $objectRevs ) . ')' ), $fname );\r
+                               while ( $row = $dbr->fetchObject( $res ) ) {\r
+                                       $oldId = $row->old_id;\r
+                                       if ( !preg_match( '/^O:(\d+):"(\w+)"/', $row->header, $matches ) ) {\r
+                                               $this->error( 'restore text', "Error: invalid object header", $oldId );\r
+                                               continue;\r
+                                       }\r
 \r
-                               $className = strtolower( $matches[2] );\r
-                               if ( strlen( $className ) != $matches[1] ) {\r
-                                       checkError( "Error: invalid object header, wrong class name length", $oldId );\r
-                                       continue;\r
-                               }\r
+                                       $className = strtolower( $matches[2] );\r
+                                       if ( strlen( $className ) != $matches[1] ) {\r
+                                               $this->error( 'restore text', "Error: invalid object header, wrong class name length", $oldId );\r
+                                               continue;\r
+                                       }\r
 \r
-                               $objectStats = $objectStats + array( $className => 0 );\r
-                               $objectStats[$className]++;\r
-\r
-                               switch ( $className ) {\r
-                                       case 'concatenatedgziphistoryblob':\r
-                                               // Good\r
-                                               break;\r
-                                       case 'historyblobstub':\r
-                                       case 'historyblobcurstub':\r
-                                               if ( strlen( $row->header ) == $headerLength ) {\r
-                                                       checkError( "Error: overlong stub header", $oldId );\r
-                                                       continue;\r
-                                               }\r
-                                               $stubObj = unserialize( $row->header );\r
-                                               if ( !is_object( $stubObj ) ) {\r
-                                                       checkError( "Error: unable to unserialize stub object", $oldId );\r
-                                                       continue;\r
-                                               }\r
-                                               if ( $className == 'historyblobstub' ) {\r
-                                                       $concatBlobs[$stubObj->mOldId][] = $oldId;\r
-                                               } else {\r
-                                                       $curIds[$stubObj->mCurId][] = $oldId;\r
-                                               }\r
-                                               break;\r
-                                       default:\r
-                                               checkError( "Error: unrecognised object class \"$className\"", $oldId );\r
+                                       $objectStats = $objectStats + array( $className => 0 );\r
+                                       $objectStats[$className]++;\r
+\r
+                                       switch ( $className ) {\r
+                                               case 'concatenatedgziphistoryblob':\r
+                                                       // Good\r
+                                                       break;\r
+                                               case 'historyblobstub':\r
+                                               case 'historyblobcurstub':\r
+                                                       if ( strlen( $row->header ) == $headerLength ) {\r
+                                                               $this->error( 'unfixable', "Error: overlong stub header", $oldId );\r
+                                                               continue;\r
+                                                       }\r
+                                                       $stubObj = unserialize( $row->header );\r
+                                                       if ( !is_object( $stubObj ) ) {\r
+                                                               $this->error( 'restore text', "Error: unable to unserialize stub object", $oldId );\r
+                                                               continue;\r
+                                                       }\r
+                                                       if ( $className == 'historyblobstub' ) {\r
+                                                               $concatBlobs[$stubObj->mOldId][] = $oldId;\r
+                                                       } else {\r
+                                                               $curIds[$stubObj->mCurId][] = $oldId;\r
+                                                       }\r
+                                                       break;\r
+                                               default:\r
+                                                       $this->error( 'unfixable', "Error: unrecognised object class \"$className\"", $oldId );\r
+                                       }\r
                                }\r
+                               $dbr->freeResult( $res );\r
                        }\r
-                       $dbr->freeResult( $res );\r
-               }\r
 \r
-               // Check local concat blob validity\r
-               $externalConcatBlobs = array();\r
-               if ( count( $concatBlobs ) ) {\r
-                       $headerLength = 300;\r
-                       $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), \r
-                               array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), $fname );\r
-                       while ( $row = $dbr->fetchObject( $res ) ) {\r
-                               $flags = explode( ',', $row->old_flags );\r
-                               if ( in_array( 'external', $flags ) ) {\r
-                                       // Concat blob is in external storage?\r
-                                       if ( in_array( 'object', $flags ) ) {\r
-                                               $urlParts = explode( '/', $row->header );\r
-                                               if ( $urlParts[0] != 'DB:' ) {\r
-                                                       checkError( "Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id );\r
-                                               } else {\r
-                                                       $cluster = $urlParts[2];\r
-                                                       $id = $urlParts[3];\r
-                                                       if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {\r
-                                                               $externalConcatBlobs[$cluster][$id] = array();\r
+                       // Check local concat blob validity\r
+                       $externalConcatBlobs = array();\r
+                       if ( count( $concatBlobs ) ) {\r
+                               $headerLength = 300;\r
+                               $res = $dbr->select( 'text', array( 'old_id', 'old_flags', "LEFT(old_text, $headerLength) AS header" ), \r
+                                       array( 'old_id IN (' . implode( ',', array_keys( $concatBlobs ) ) . ')' ), $fname );\r
+                               while ( $row = $dbr->fetchObject( $res ) ) {\r
+                                       $flags = explode( ',', $row->old_flags );\r
+                                       if ( in_array( 'external', $flags ) ) {\r
+                                               // Concat blob is in external storage?\r
+                                               if ( in_array( 'object', $flags ) ) {\r
+                                                       $urlParts = explode( '/', $row->header );\r
+                                                       if ( $urlParts[0] != 'DB:' ) {\r
+                                                               $this->error( 'unfixable', "Error: unrecognised external storage type \"{$urlParts[0]}", $row->old_id );\r
+                                                       } else {\r
+                                                               $cluster = $urlParts[2];\r
+                                                               $id = $urlParts[3];\r
+                                                               if ( !isset( $externalConcatBlobs[$cluster][$id] ) ) {\r
+                                                                       $externalConcatBlobs[$cluster][$id] = array();\r
+                                                               }\r
+                                                               $externalConcatBlobs[$cluster][$id] = array_merge( \r
+                                                                       $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]\r
+                                                               );\r
                                                        }\r
-                                                       $externalConcatBlobs[$cluster][$id] = array_merge( \r
-                                                               $externalConcatBlobs[$cluster][$id], $concatBlobs[$row->old_id]\r
-                                                       );\r
+                                               } else {\r
+                                                       $this->error( 'unfixable', "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",\r
+                                                               $concatBlobs[$row->old_id] );\r
                                                }\r
-                                       } else {\r
-                                               checkError( "Error: invalid flags \"{$row->old_flags}\" on concat bulk row {$row->old_id}",\r
+                                       } elseif ( strcasecmp( substr( $row->header, 0, strlen( CONCAT_HEADER ) ), CONCAT_HEADER ) ) {\r
+                                               $this->error( 'restore text', "Error: Incorrect object header for concat bulk row {$row->old_id}", \r
                                                        $concatBlobs[$row->old_id] );\r
-                                       }\r
-                               } elseif ( strcasecmp( substr( $row->header, 0, strlen( CONCAT_HEADER ) ), CONCAT_HEADER ) ) {\r
-                                       checkError( "Error: Incorrect object header for concat bulk row {$row->old_id}", \r
-                                               $concatBlobs[$row->old_id] );\r
-                               } # else good\r
+                                       } # else good\r
 \r
-                               unset( $concatBlobs[$row->old_id] );\r
+                                       unset( $concatBlobs[$row->old_id] );\r
+                               }\r
+                               $dbr->freeResult( $res );\r
                        }\r
-                       $dbr->freeResult( $res );\r
-               }\r
 \r
-               // Check targets of unresolved stubs\r
-               checkExternalConcatBlobs( $externalConcatBlobs );\r
-               $dbr->ping();\r
+                       // Check targets of unresolved stubs\r
+                       $this->checkExternalConcatBlobs( $externalConcatBlobs );\r
 \r
-               // next chunk\r
-       }\r
+                       // next chunk\r
+               }\r
 \r
-       print "\n\n" . count( $brokenRevisions ) . " broken revisions\n";\r
+               print "\n\nErrors:\n";\r
+               foreach( $this->errors as $name => $errors ) {\r
+                       if ( count( $errors ) ) {\r
+                               $description = $this->errorDescriptions[$name];\r
+                               echo "$description: " . implode( ',', array_keys( $errors ) ) . "\n";\r
+                       }\r
+               }\r
 \r
-       print "\nFlag statistics:\n";\r
-       $total = array_sum( $flagStats );\r
-       foreach ( $flagStats as $flag => $count ) {\r
-               printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );\r
-       }\r
-       print "\nObject statistics:\n";\r
-       $total = array_sum( $objectStats );\r
-       foreach ( $objectStats as $className => $count ) {\r
-               printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );\r
+               if ( count( $this->errors['restore text'] ) && $fix ) {\r
+                       if ( (string)$xml !== '' ) {\r
+                               $this->restoreText( array_keys( $this->errors['restore text'] ), $xml );\r
+                       } else {\r
+                               echo "Can't fix text, no XML backup specified\n";\r
+                       }\r
+               }\r
+\r
+               print "\nFlag statistics:\n";\r
+               $total = array_sum( $flagStats );\r
+               foreach ( $flagStats as $flag => $count ) {\r
+                       printf( "%-30s %10d %5.2f%%\n", $flag, $count, $count / $total * 100 );\r
+               }\r
+               print "\nLocal object statistics:\n";\r
+               $total = array_sum( $objectStats );\r
+               foreach ( $objectStats as $className => $count ) {\r
+                       printf( "%-30s %10d %5.2f%%\n", $className, $count, $count / $total * 100 );\r
+               }\r
        }\r
-}\r
 \r
 \r
-function checkError( $msg, $ids ) {\r
-       global $oldIdMap, $brokenRevisions;\r
-       if ( is_array( $ids ) && count( $ids ) == 1 ) {\r
-               $ids = reset( $ids );\r
-       }\r
-       if ( is_array( $ids ) ) {\r
-               $revIds = array();\r
-               foreach ( $ids as $id ) {\r
-                       $revIds = array_merge( $revIds, array_keys( $oldIdMap, $id ) );\r
+       function error( $type, $msg, $ids ) {\r
+               if ( is_array( $ids ) && count( $ids ) == 1 ) {\r
+                       $ids = reset( $ids );\r
                }\r
-               print "$msg in text rows " . implode( ', ', $ids ) . \r
-                       ", revisions " . implode( ', ', $revIds ) . "\n";\r
-       } else {\r
-               $id = $ids;\r
-               $revIds = array_keys( $oldIdMap, $id );\r
-               if ( count( $revIds ) == 1 ) {\r
-                       print "$msg in old_id $id, rev_id {$revIds[0]}\n";\r
+               if ( is_array( $ids ) ) {\r
+                       $revIds = array();\r
+                       foreach ( $ids as $id ) {\r
+                               $revIds = array_merge( $revIds, array_keys( $this->oldIdMap, $id ) );\r
+                       }\r
+                       print "$msg in text rows " . implode( ', ', $ids ) . \r
+                               ", revisions " . implode( ', ', $revIds ) . "\n";\r
                } else {\r
-                       print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";\r
+                       $id = $ids;\r
+                       $revIds = array_keys( $this->oldIdMap, $id );\r
+                       if ( count( $revIds ) == 1 ) {\r
+                               print "$msg in old_id $id, rev_id {$revIds[0]}\n";\r
+                       } else {\r
+                               print "$msg in old_id $id, revisions " . implode( ', ', $revIds ) . "\n";\r
+                       }\r
                }\r
+               $this->errors[$type] = $this->errors[$type] + array_flip( $revIds );\r
        }\r
-       $brokenRevisions = $brokenRevisions + array_flip( $revIds );\r
-}\r
 \r
-function checkExternalConcatBlobs( $externalConcatBlobs ) {\r
-       static $dbStore = null;\r
-       $fname = 'checkExternalConcatBlobs';\r
-       if ( !count( $externalConcatBlobs ) ) {\r
-               return;\r
+       function checkExternalConcatBlobs( $externalConcatBlobs ) {\r
+               $fname = 'CheckStorage::checkExternalConcatBlobs';\r
+               if ( !count( $externalConcatBlobs ) ) {\r
+                       return;\r
+               }\r
+\r
+               if ( is_null( $this->dbStore ) ) {\r
+                       $this->dbStore = new ExternalStoreDB;\r
+               }\r
+               \r
+               foreach ( $externalConcatBlobs as $cluster => $oldIds ) {\r
+                       $blobIds = array_keys( $oldIds );\r
+                       $extDb =& $this->dbStore->getSlave( $cluster );\r
+                       $blobsTable = $this->dbStore->getTable( $extDb );\r
+                       $headerLength = strlen( CONCAT_HEADER );\r
+                       $res = $extDb->select( $blobsTable, \r
+                               array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ), \r
+                               array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );\r
+                       while ( $row = $extDb->fetchObject( $res ) ) {\r
+                               if ( strcasecmp( $row->header, CONCAT_HEADER ) ) {\r
+                                       $this->error( 'restore text', "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL", \r
+                                               $oldIds[$row->blob_id] );\r
+                               }\r
+                               unset( $oldIds[$row->blob_id] );\r
+\r
+                       }\r
+                       $extDb->freeResult( $res );\r
+\r
+                       // Print errors for missing blobs rows\r
+                       foreach ( $oldIds as $blobId => $oldIds ) {\r
+                               $this->error( 'restore text', "Error: missing target $cluster/$blobId for two-part ES URL", $oldIds );\r
+                       }\r
+               }\r
        }\r
 \r
-       if ( is_null( $dbStore ) ) {\r
-               $dbStore = new ExternalStoreDB;\r
+       function restoreText( $revIds, $xml ) {\r
+               global $wgTmpDirectory, $wgDBname;\r
+\r
+               if ( !count( $revIds ) ) {\r
+                       return;\r
+               }\r
+\r
+               print "Restoring text from XML backup...\n";\r
+\r
+               $revFileName = "$wgTmpDirectory/broken-revlist-$wgDBname";\r
+               $filteredXmlFileName = "$wgTmpDirectory/filtered-$wgDBname.xml";\r
+               \r
+               // Write revision list\r
+               if ( !file_put_contents( $revFileName, implode( "\n", $revIds ) ) ) {\r
+                       echo "Error writing revision list, can't restore text\n";\r
+                       return;\r
+               }\r
+\r
+               // Run mwdumper\r
+               echo "Filtering XML dump...\n";\r
+               $exitStatus = 0;\r
+               passthru( 'mwdumper ' . \r
+                       wfEscapeShellArg( \r
+                               "--output=file:$filteredXmlFileName",\r
+                               "--filter=revlist:$revFileName",\r
+                               $xml\r
+                       ), $exitStatus\r
+               );\r
+\r
+               if ( $exitStatus ) {\r
+                       echo "mwdumper died with exit status $exitStatus\n";\r
+                       return;\r
+               }\r
+\r
+               $file = fopen( $filteredXmlFileName, 'r' );\r
+               if ( !$file ) {\r
+                       echo "Unable to open filtered XML file\n";\r
+                       return;\r
+               }\r
+\r
+               $dbr =& wfGetDB( DB_SLAVE );\r
+               $dbw =& wfGetDB( DB_MASTER );\r
+               $dbr->ping();\r
+               $dbw->ping();\r
+               \r
+               $source = new ImportStreamSource( $file );\r
+               $importer = new WikiImporter( $source );\r
+               $importer->setRevisionCallback( array( &$this, 'importRevision' ) );\r
+               $importer->doImport();\r
        }\r
-       \r
-       foreach ( $externalConcatBlobs as $cluster => $oldIds ) {\r
-               $blobIds = array_keys( $oldIds );\r
-               $extDb =& $dbStore->getSlave( $cluster );\r
-               $blobsTable = $dbStore->getTable( $extDb );\r
-               $headerLength = strlen( CONCAT_HEADER );\r
-               $res = $extDb->select( $blobsTable, \r
-                       array( 'blob_id', "LEFT(blob_text, $headerLength) AS header" ), \r
-                       array( 'blob_id IN( ' . implode( ',', $blobIds ) . ')' ), $fname );\r
-               while ( $row = $extDb->fetchObject( $res ) ) {\r
-                       if ( strcasecmp( $row->header, CONCAT_HEADER ) ) {\r
-                               checkError( "Error: invalid header on target $cluster/{$row->blob_id} of two-part ES URL", \r
-                                       $oldIds[$row->blob_id] );\r
-                       }\r
-                       unset( $oldIds[$row->blob_id] );\r
 \r
+       function importRevision( &$revision, &$importer ) {\r
+               $fname = 'CheckStorage::importRevision';\r
+\r
+               $id = $revision->getID();\r
+               $text = $revision->getText();\r
+               if ( $text === '' ) {\r
+                       // This is what happens if the revision was broken at the time the \r
+                       // dump was made. Unfortunately, it also happens if the revision was \r
+                       // legitimately blank, so there's no way to tell the difference. To\r
+                       // be safe, we'll skip it and leave it broken\r
+                       $id = $id ? $id : '';\r
+                       echo "Revision $id is blank in the dump, may have been broken before export\n";\r
+                       return;\r
                }\r
-               $extDb->freeResult( $res );\r
 \r
-               // Print errors for missing blobs rows\r
-               foreach ( $oldIds as $blobId => $oldIds ) {\r
-                       checkError( "Error: missing target $cluster/$blobId for two-part ES URL", $oldIds );\r
+               if ( !$id )  {\r
+                       // No ID, can't import\r
+                       echo "No id tag in revision, can't import\n";\r
+                       return;\r
                }\r
+\r
+               // Find text row again\r
+               $dbr =& wfGetDB( DB_SLAVE );\r
+               $oldId = $dbr->selectField( 'revision', 'rev_text_id', array( 'rev_id' => $id ), $fname );\r
+               if ( !$oldId ) {\r
+                       echo "Missing revision row for rev_id $id\n";\r
+                       return;\r
+               }\r
+\r
+               // Compress the text\r
+               $flags = Revision::compressRevisionText( $text );\r
+\r
+               // Update the text row\r
+               $dbw->update( 'text', \r
+                       array( 'old_flags' => $flags, 'old_text' => $text ),\r
+                       array( 'old_id' => $oldId ),\r
+                       $fname, array( 'LIMIT' => 1 )\r
+               );\r
+\r
+               // Remove it from the unfixed list and add it to the fixed list\r
+               unset( $this->errors['restore text'][$id] );\r
+               $this->errors['fixed'][$id] = true;\r
        }\r
 }\r
-\r
 ?>\r